import { goToPage } from "@/utils/common";
import { Toast } from "@nutui/nutui-react-taro";
import { useEffect, useState } from "react";

export default function useTaskcenter() {
  const [list, setList] = useState<string[]>([]);
  const [hasMore, setHasMore] = useState(true);

  const sleep = (time: number): Promise<unknown> => {
    return new Promise((resolve) => {
      setTimeout(resolve, time);
    });
  };
  const init = () => {
    for (let i = 0; i < 10; i++) {
      list.push(`${i}`);
    }
    setList([...list]);
  };

  useEffect(() => {
    init();
  }, []);

  /**
   * 上拉加载
   */
  const onHandleLoadMore = async () => {
    await sleep(2000);
    const curLen = list.length;
    for (let i = curLen; i < curLen + 10; i++) {
      list.push(`${i}`);
    }
    if (list.length >= 30) {
      setHasMore(false);
    } else {
      setList([...list]);
    }
  };

  /**
   * 下拉刷新操作
   */
  const onHandleRefresh = async () => {
    await sleep(1000);
    const newList: any[] = [];
    for (let i = 0; i < 10; i++) {
      newList.push(`${i}`);
    }
    setHasMore(false);
    setList([...newList]);
  };

  const showDetail = (options: any) => {
    goToPage("pages/taskdetail/index");
  };

  return {
    list,
    hasMore,
    onHandleRefresh,
    onHandleLoadMore,
    showDetail,
  };
}
